import { _decorator, Component, Node, director, game } from 'cc';
import { AudioManager } from './AudioManager';
const { ccclass, property } = _decorator;

/**
 * 场景管理器
 * 负责处理场景切换
 */
@ccclass('SceneManager')
export class SceneManager extends Component {
    private static _instance: SceneManager = null;
    
    public static get instance(): SceneManager {
        return this._instance;
    }
    
    // 场景名称常量
    public static readonly SCENE_MAIN_MENU = 'MainMenu';
    public static readonly SCENE_GAME = 'Game';
    public static readonly SCENE_GAME_OVER = 'GameOver';
    
    // 当前场景名称
    private _currentScene: string = '';
    
    // 记录加载状态，避免重复加载
    private _isLoading: boolean = false;
    
    // 生命周期方法
    onLoad() {
        console.log("SceneManager onLoad 执行");
        // 单例模式，但不设为常驻节点
        if (SceneManager._instance === null) {
            SceneManager._instance = this;
            console.log("SceneManager 已初始化");
        } else {
            console.log("已存在 SceneManager 实例，销毁当前节点");
            this.node.destroy();
            return;
        }
    }
    
    onDestroy() {
        // 当节点被销毁时清除单例引用
        if (SceneManager._instance === this) {
            console.log("SceneManager 实例被销毁");
            SceneManager._instance = null;
        }
    }
    
    /**
     * 加载场景
     * @param sceneName 场景名称
     * @param onComplete 加载完成回调
     */
    public loadScene(sceneName: string, onComplete?: () => void): void {
        // 避免重复加载相同场景或加载中重复触发
        if (this._isLoading || sceneName === this._currentScene) {
            console.log(`场景加载取消: 当前正在加载或已经是场景 ${sceneName}`);
            return;
        }
        
        console.log(`开始加载场景: ${sceneName}`);
        this._isLoading = true;
        
        // 预先检查资源是否可用
        director.preloadScene(sceneName, (err) => {
            if (err) {
                console.error(`预加载场景失败: ${sceneName}`, err);
                this._isLoading = false;
                return;
            }
            
            // 正式加载场景
            director.loadScene(sceneName, (err) => {
                this._isLoading = false;
                
                if (err) {
                    console.error(`加载场景失败: ${sceneName}`, err);
                    return;
                }
                
                // 更新当前场景记录
                this._currentScene = sceneName;
                console.log(`场景加载完成: ${sceneName}`);
                
                // 播放场景对应的背景音乐
                this.playSceneMusic(sceneName);
                
                // 回调
                if (onComplete) {
                    onComplete();
                }
            });
        });
    }
    
    /**
     * 播放场景对应的背景音乐
     */
    private playSceneMusic(sceneName: string): void {
        if (!AudioManager.instance) return;
        
        // 根据场景播放不同的背景音乐
        switch (sceneName) {
            case SceneManager.SCENE_MAIN_MENU:
                AudioManager.instance.playMusic('bgm_menu');
                break;
            case SceneManager.SCENE_GAME:
                AudioManager.instance.playMusic('bgm_game');
                break;
            case SceneManager.SCENE_GAME_OVER:
                AudioManager.instance.playMusic('bgm_gameover');
                break;
            default:
                // 默认音乐
                AudioManager.instance.playMusic('bgm_game');
                break;
        }
    }
    
    /**
     * 加载主菜单场景
     */
    public loadMainMenu(): void {
        this.loadScene(SceneManager.SCENE_MAIN_MENU);
    }
    
    /**
     * 加载游戏场景
     */
    public loadGame(): void {
        this.loadScene(SceneManager.SCENE_GAME);
    }
    
    /**
     * 加载游戏结束场景
     */
    public loadGameOver(): void {
        this.loadScene(SceneManager.SCENE_GAME_OVER);
    }
    
    /**
     * 获取当前场景名称
     */
    public getCurrentScene(): string {
        return this._currentScene;
    }
} 